package com.smart.modules.genealogy.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.smart.modules.genealogy.domain.Genealogy;
import com.smart.modules.genealogy.domain.PersonRelationship;
import com.smart.modules.genealogy.dto.GenealogyPersonDto;
import com.smart.modules.genealogy.mapper.GenealogyMapper;
import com.smart.modules.genealogy.mapper.PersonRelationshipMapper;
import com.smart.modules.genealogy.vo.GenealogyDataTreeVo;
import com.smart.system.common.result.Result;
import com.smart.system.common.util.DateUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.smart.modules.genealogy.mapper.GenealogyPersonMapper;
import com.smart.modules.genealogy.domain.GenealogyPerson;
import com.smart.modules.genealogy.service.IGenealogyPersonService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 家谱人员Service业务层处理
 */
@Service
public class GenealogyPersonServiceImpl implements IGenealogyPersonService
{
    @Autowired
    private GenealogyPersonMapper genealogyPersonMapper;
    @Autowired
    private GenealogyMapper genealogyMapper;
    @Autowired
    private PersonRelationshipMapper personRelationshipMapper;

    /**
     * 查询家谱人员
     *
     * @param genealogyPersonId 家谱人员主键
     * @return 家谱人员
     */
    @Override
    public GenealogyPerson selectGenealogyPersonByGenealogyPersonId(Long genealogyPersonId)
    {
        return genealogyPersonMapper.selectGenealogyPersonByGenealogyPersonId(genealogyPersonId);
    }

    /**
     * 查询家谱人员列表
     *
     * @param genealogyPerson 家谱人员
     * @return 家谱人员
     */
    @Override
    public IPage<GenealogyPerson> selectGenealogyPersonList(IPage<GenealogyPerson> page,GenealogyPerson genealogyPerson)
    {
        return genealogyPersonMapper.selectGenealogyPersonList(page,genealogyPerson);
    }

    /**
     * 新增家谱人员
     *
     * @param genealogyPersonDto 家谱人员
     * @return 结果
     */
    @Override
    @Transactional
    public int insertGenealogyPerson(GenealogyPersonDto genealogyPersonDto)
    {
        GenealogyPerson genealogyPerson = new GenealogyPerson();
        BeanUtils.copyProperties(genealogyPersonDto,genealogyPerson);
        genealogyPerson.setCreateTime(DateUtils.getNowDate());
        genealogyPersonMapper.insertGenealogyPerson(genealogyPerson);
        Long genealogyPersonId = genealogyPerson.getGenealogyPersonId();
        Long genealogyPersonPid = genealogyPerson.getGenealogyPersonPid();
        if (genealogyPersonPid !=0){
            PersonRelationship personRelationship = new PersonRelationship();
            personRelationship.setPersonId(genealogyPersonId);
            personRelationship.setPersonId2(genealogyPersonPid);
            personRelationship.setRelationshipTypeId(genealogyPersonDto.getRelationshipTypeId());
            personRelationshipMapper.insertPersonRelationship(personRelationship);
        }
        return 1;
    }

    /**
     * 修改家谱人员
     *
     * @param genealogyPerson 家谱人员
     * @return 结果
     */
    @Override
    public int updateGenealogyPerson(GenealogyPerson genealogyPerson)
    {
        genealogyPerson.setUpdateTime(DateUtils.getNowDate());
        return genealogyPersonMapper.updateGenealogyPerson(genealogyPerson);
    }

    /**
     * 批量删除家谱人员
     *
     * @param genealogyPersonIds 需要删除的家谱人员主键
     * @return 结果
     */
    @Override
    public int deleteGenealogyPersonByGenealogyPersonIds(Long[] genealogyPersonIds)
    {
        return genealogyPersonMapper.deleteGenealogyPersonByGenealogyPersonIds(genealogyPersonIds);
    }

    /**
     * 删除家谱人员信息
     *
     * @param genealogyPersonId 家谱人员主键
     * @return 结果
     */
    @Override
    public int deleteGenealogyPersonByGenealogyPersonId(Long genealogyPersonId)
    {
        return genealogyPersonMapper.deleteGenealogyPersonByGenealogyPersonId(genealogyPersonId);
    }

    @Override
    public Result buildGenealogyByGenealogyId(Long genealogyId) {
        GenealogyDataTreeVo tree = new GenealogyDataTreeVo();
        // 构建父节点
        Genealogy genealogy = genealogyMapper.selectGenealogyByGenealogyId(genealogyId);
        tree.setGenealogyId(genealogy.getGenealogyId());
        tree.setGenealogyPersonId(0l);
        tree.setImage_url("https://element-plus.org/images/element-plus-logo.svg");
        tree.setName(genealogy.getGenealogyName());// 族谱名称
        List<GenealogyPerson> genealogyPersonList = genealogyPersonMapper.selectGenealogyPersonListByGenealogyId(genealogyId);
        List<GenealogyDataTreeVo> topChildrenList = new ArrayList<>(); // 父节点children
        List<GenealogyPerson> pList = genealogyPersonList.stream().filter(i -> i.getGenealogyPersonPid() == 0).toList(); // 父节点
        for (GenealogyPerson genealogyPerson : pList) {
            GenealogyDataTreeVo childrenData = new GenealogyDataTreeVo();
            Long genealogyPersonId = genealogyPerson.getGenealogyPersonId();
            String personName = genealogyPerson.getPersonName();
            childrenData.setGenealogyId(genealogyId);
            childrenData.setName(personName);
            childrenData.setImage_url(genealogyPerson.getPersonAvatar());
            childrenData.setGenealogyPersonId(genealogyPersonId);
            setGenealogyChildren(childrenData);
            topChildrenList.add(childrenData);
        }
        tree.setChildren(topChildrenList);
        return Result.success(tree);
    }

    public void setGenealogyChildren(GenealogyDataTreeVo childrenData){
        Long genealogyPersonId = childrenData.getGenealogyPersonId();
        List<GenealogyDataTreeVo> genealogyChildren = getGenealogyChildren(genealogyPersonId);
        if (!genealogyChildren.isEmpty()){
            for (GenealogyDataTreeVo genealogyChild : genealogyChildren) {
                setGenealogyChildren(genealogyChild);
            }
        }
        childrenData.setChildren(genealogyChildren);
    }
    public List<GenealogyDataTreeVo> getGenealogyChildren(Long pid){
        List<GenealogyDataTreeVo> treeChildren = new ArrayList<>();
        List<GenealogyPerson> genealogyPersonList = genealogyPersonMapper.selectGenealogyPersonListByPersonId(pid);
        for (GenealogyPerson genealogyPerson : genealogyPersonList) {
            GenealogyDataTreeVo genealogyDataTreeVo = new GenealogyDataTreeVo();
            genealogyDataTreeVo.setName(genealogyPerson.getPersonName());
            genealogyDataTreeVo.setGenealogyPersonId(genealogyPerson.getGenealogyPersonId());
            genealogyDataTreeVo.setGenealogyId(genealogyPerson.getGenealogyId());
            genealogyDataTreeVo.setImage_url(genealogyPerson.getPersonAvatar());
            treeChildren.add(genealogyDataTreeVo);
        }
        return treeChildren;
    }
}
